/*
 * Main.c
 *
 *  Created on: 16 juli 2021
 *      Author: Daniel Mårtensson
 */

#include <stdio.h>

 /* Include Open SAE J1939 */
#include "Open_SAE_J1939/Open_SAE_J1939.h"

int main() {

	/* Create our J1939 structure with two ECU */
	J1939 j1939_1 = { 0 };
	J1939 j1939_2 = { 0 };

	/* Important to sent all non-address to 0xFF - Else we cannot use ECU address 0x0 */
	uint8_t i;
	for (i = 0; i < 255; i++) {
		j1939_1.other_ECU_address[i] = 0xFF;
		j1939_2.other_ECU_address[i] = 0xFF;
	}

	/* Set the ECU address */
	j1939_1.information_this_ECU.this_ECU_address = 0x80; /* From 0 to 253 because 254 = error address and 255 = broadcast address */
	j1939_2.information_this_ECU.this_ECU_address = 0x90;

	/* Set the DM1 error message - You can have multiple error messages, but only send one at the time */
	j1939_1.this_dm.dm1.SAE_lamp_status_malfunction_indicator = 1;
	j1939_1.this_dm.dm1.SAE_lamp_status_red_stop = 0;
	j1939_1.this_dm.dm1.SAE_lamp_status_amber_warning = 1;
	j1939_1.this_dm.dm1.SAE_lamp_status_protect_lamp = 0;
	j1939_1.this_dm.dm1.SAE_flash_lamp_malfunction_indicator = 0;
	j1939_1.this_dm.dm1.SAE_flash_lamp_red_stop = 1;
	j1939_1.this_dm.dm1.SAE_flash_lamp_amber_warning = 0;
	j1939_1.this_dm.dm1.SAE_flash_lamp_protect_lamp = 1;

	j1939_1.this_dm.dm1.FMI[0] = FMI_CURRENT_ABOVE_NORMAL; /* If FMI_NOT_AVAILABLE, then errors_dm1_active will become 0 */
	j1939_1.this_dm.dm1.SPN[0] = SPN_5_VOLTS_DC_SUPPLY;
	j1939_1.this_dm.dm1.SPN_conversion_method[0] = 1;
	j1939_1.this_dm.dm1.occurrence_count[0] = 1;

	j1939_1.this_dm.dm1.FMI[1] = FMI_VOLTAGE_BELOW_NORMAL; /* If FMI_NOT_AVAILABLE, then errors_dm1_active will become 0 */
	j1939_1.this_dm.dm1.SPN[1] = SPN_SENSOR_SUPPLY_VOLTAGE_1;
	j1939_1.this_dm.dm1.SPN_conversion_method[1] = 1;
	j1939_1.this_dm.dm1.occurrence_count[1] = 2;

	j1939_1.this_dm.dm1.FMI[2] = FMI_ABOVE_NORMAL_MODERATELY_SEVERE; /* If FMI_NOT_AVAILABLE, then errors_dm1_active will become 0 */
	j1939_1.this_dm.dm1.SPN[2] = SPN_ENGINE_COOLANT_LEVEL;
	j1939_1.this_dm.dm1.SPN_conversion_method[2] = 1;
	j1939_1.this_dm.dm1.occurrence_count[2] = 3;

	j1939_1.this_dm.errors_dm1_active = 3; /* If this is above 1, then this is going to be send as multi-packet */

	/* Request DM1 codes from ECU 2 to ECU 1 */
	SAE_J1939_Send_Request(&j1939_2, 0x80, PGN_DM1);

	/* Read */
	for (i = 0; i < 10; i++) {
		Open_SAE_J1939_Listen_For_Messages(&j1939_1); 
		Open_SAE_J1939_Listen_For_Messages(&j1939_2);
	}

	/* Display what ECU 2 got */
	printf("Active DTCs = %i\n", j1939_2.from_other_ecu_dm.errors_dm1_active);
	printf("SAE lamp status malfunction indicator = %i\nSAE lamp status red stop = %i\nSAE lamp status amber warning = %i\nSAE lamp status protect lamp = %i\nSAE flash lamp malfunction indicator = %i\nSAE flash lamp_red stop = %i\nSAE flash lamp amber warning = %i\nSAE flash lamp protect lamp = %i\n",
		j1939_2.from_other_ecu_dm.dm1.SAE_lamp_status_malfunction_indicator,
		j1939_2.from_other_ecu_dm.dm1.SAE_lamp_status_red_stop,
		j1939_2.from_other_ecu_dm.dm1.SAE_lamp_status_amber_warning,
		j1939_2.from_other_ecu_dm.dm1.SAE_lamp_status_protect_lamp,
		j1939_2.from_other_ecu_dm.dm1.SAE_flash_lamp_malfunction_indicator,
		j1939_2.from_other_ecu_dm.dm1.SAE_flash_lamp_red_stop,
		j1939_2.from_other_ecu_dm.dm1.SAE_flash_lamp_amber_warning,
		j1939_2.from_other_ecu_dm.dm1.SAE_flash_lamp_protect_lamp);

	for (i = 0; i < j1939_2.from_other_ecu_dm.errors_dm1_active; i++) {
		printf("\nDTC %i\n FMI = %i\n SPN = %i\n SPN conversion method = %i\n Occurrence_count = %i\n From ECU address 0x%X\n",
			i, j1939_2.from_other_ecu_dm.dm1.FMI[i],
			j1939_2.from_other_ecu_dm.dm1.SPN[i],
			j1939_2.from_other_ecu_dm.dm1.SPN_conversion_method[i],
			j1939_2.from_other_ecu_dm.dm1.occurrence_count[i],
			j1939_2.from_other_ecu_dm.dm1.from_ecu_address[i]);
	}
	return 0;
}
